# -*- coding:utf-8 -*-
import scrapy
from python_scrapy.items import DoubanItem


class DoubanSpiderSpider(scrapy.Spider):
    # 爬虫名字
    name = 'douban_spider'
    # 允许的域名
    allowed_domains = ['movie.douban.com']
    # 入口url，扔到调度器中
    start_urls = ['https://movie.douban.com/top250']

    # 默认的解析方法
    def parse(self, response):
        # yield 返回的是一个迭代器
        # extract() 返回的所有数据，存在一个list里。
        # extract_first() 返回的是一个string，是extract()结果中第一个值。
        # print("输出信息是：", response.text)
        movie_list = response.xpath("//div[@class='article']//ol[@class='grid_view']/li")
        for item in movie_list:
            douban_item = DoubanItem()
            douban_item["serial_number"] = item.xpath(".//div[@class='item']/div[@class='pic']/em/text()").extract_first()
            douban_item["movie_name"] = item.xpath(".//div[@class='item']/div[@class='info']/div[@class='hd']/a/span[@class='title']/text()").extract_first()
            # douban_item["movie_name"] = item.xpath("//div[@class='article']//ol[@class='grid_view']/li/div[@class='item']/div[@class='pic']/a/img/@alt").extract_first()
            douban_item["front_cover"] = item.xpath(".//div[@class='item']/div[@class='pic']/a/img/@src").extract_first()
            douban_item["star"] = item.xpath(".//div[@class='item']/div[@class='info']/div[@class='bd']/div[@class='star']/span[@class='rating_num']/text()").extract_first()
            douban_item["evaluate"] = item.xpath(".//div[@class='item']/div[@class='info']/div[@class='bd']/div[@class='star']/span[4]/text()").extract_first()
            douban_item["describe"] = item.xpath(".//div[@class='item']/div[@class='info']/div[@class='bd']/p[@class='quote']/span[@class='inq']/text()").extract_first()

            content = item.xpath(".//div[@class='item']/div[@class='info']/div[@class='bd']/p[1]/text()").extract()
            for i_content in content:
                content_s = "".join(i_content.split())
                douban_item["introduce"] = content_s

            # 将数据yield到pipelines里去
            yield douban_item

        # 解析下一页规则
        next_link = response.xpath("//span[@class='next']/link/@href").extract()
        if next_link:
            next_link = next_link[0]
            yield scrapy.Request("https://movie.douban.com/top250" + next_link, callback=self.parse)
        pass
